import {
    vipRoomUrl
} from '../../../config/request.config.js'
const api = {
    search: 'viproom/StationScreen/getScreenList',
    SelectStation: 'viproom/station/selectStation'
}
export default {
    data() {
        return {
            stationList: [],
            stationId: '',
            pageIndex: 1,
            pageSize: 10,
            pageCount: 1,
            total: 0,
            tableData: [],
            circleFlag: null,
            btnText: '全屏'
        }
    },
    methods: {
        searchStation() {
            $.ajax({
                url: vipRoomUrl + api.SelectStation,
                data: JSON.stringify({
                    typeId:1,
                    stationType:0,
                    useRange:1
                }),
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                success: res => {
                    if (res && res.code === '0000' && res.result) {
                        this.stationList = res.result.list || []
                    }
                }
            })
        },
        searchList() {
            $.ajax({
                url: vipRoomUrl + api.search,
                data: JSON.stringify({
                    stationId: this.stationId,
                    pageIndex: this.pageIndex,
                    pageSize: this.pageSize
                }),
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                success: res => {
                    if (res && res.code === '0000' && res.result.list && res.result.list.length > 0) {
                        let systime = res.result.list[0].systemTime
                        for (let i = 0, len = res.result.list.length; i < len; i++) {
                            res.result.list[i].countdown = this.getCountdown(systime, res.result.list[i].trainStartTime)
                        }
                        this.pageCount = Math.ceil(res.result.totalCount / res.result.pageSize)
                        this.tableData = res.result.list
                        this.pageSize = res.result.pageSize
                        this.total = res.result.totalCount
                        // this.pageIndex = res.result.pageIndex
                        if (this.pageIndex >= this.pageCount) {
                            this.pageIndex = 1
                        } else {
                            this.pageIndex++
                        }
                    } else {
                        this.pageIndex = 1
                        this.pageSize = 10
                        this.pageCount = 1
                        this.total = 0
                        // this.tableData = []
                        // this.searchList()
                    }
                }
            })
        },
        rowStyle(data, rowIndex) {
            let obj = {
                'background-color': '#303133',
                'color': '#fff'
            }
            if (data.row.countdown.totalMin <= 20) {
                obj['color'] = '#F56C6C'
                if(data.row.countdown.totalMin == 0){
                    obj['display'] = 'none'
                }
            }
            return obj
        },
        headStyle() {
            return {
                'color': '#000'
            }
        },
        getCountdown(systime, endTime) {
            let res = 0
            if (systime && endTime) {
                endTime = new Date(endTime).getTime()
                res = endTime - systime
            }
            if (res && res > 0) {
                let totalMin = (res / 1000 / 60).toFixed(0)
                let hour = Math.floor(totalMin / 60) 
                let min = totalMin % 60
                let timeStr = ''
                if(hour > 0){
                    timeStr = hour + ' 时 ' + min + ' 分'
                } else {
                    timeStr = totalMin + ' 分'
                }
                return { timeStr, totalMin }
            }
            return '已发车'
        },
        circleRun() {
            if (this.circleFlag) {
                clearInterval(this.circleFlag)
            }
            this.tableData = []
            this.pageIndex = 1
            this.pageCount = 1
            this.total = 0
            this.searchList()
            this.circleFlag = setInterval(() => {
                this.searchList()
            }, 15000)
        },
        toggleFullScreen() {
            if (!document.fullscreenElement && // alternative standard method
                !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) { // current working methods
                if (document.documentElement.requestFullscreen) {
                    document.documentElement.requestFullscreen();
                } else if (document.documentElement.msRequestFullscreen) {
                    document.documentElement.msRequestFullscreen();
                } else if (document.documentElement.mozRequestFullScreen) {
                    document.documentElement.mozRequestFullScreen();
                } else if (document.documentElement.webkitRequestFullscreen) {
                    document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
                }
                this.btnText = '退出'
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                }
                this.btnText = '全屏'
            }
        }
    },
    mounted() {
        this.searchStation()
    }
}